#include <cctype>
#include <iostream>
#include <vector>
#include <string>
#include <fstream>
#include <sstream>
#include <map>
class Dictionary
{
public:
    void read(const std::string &filename)
    {
        std::ifstream ifs(filename);
        if (!ifs)
        {
            std::cerr << "ifstream open file failed" << std::endl;
            return;
        }
        std::string line;
        while (std::getline(ifs, line))
        {
            std::istringstream iss(line);
            std::string word;
            while (iss >> word)
            {
                dealWord(word);
                insert(word);
            }
        }
        ifs.close();
    }
    void dealWord(std::string &word)
    {
        auto it = word.begin();
        while (it != word.end())
        {
            if (!isalpha(*it))
            {
                it = word.erase(it);
            }
            else
            {
                ++it;
            }
        }
    }
    void insert(const std::string &word)
    {
        if (word.size() == 0)
        {
            return;
        }
        // std::map<std::string, int>::iterator pos = _dict.find(word);
        // if (pos != _dict.end())
        // {
        //     pos->second++;
        // }
        // else
        // {
        //     _dict.insert(make_pair(word, 1));
        // }
        ++_dict[word];
    }
    void store(const std::string &filename)
    {
        std::ofstream ofs(filename);
        if (!ofs)
        {
            std::cerr << "ofstream open file failed" << std::endl;
            return;
        }
        for (auto &re : _dict)
        {
            ofs << re.first << " " << re.second << std::endl;
        }
        ofs.close();
    }

private:
    std::map<std::string, int> _dict;
};
void test()
{
    Dictionary d1;
    d1.read("dic.txt");
    d1.store("diccat.txt");
}
int main(int argc, char *argv[])
{
    test();
    return 0;
}